package com.heima.user.service.impl;


import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.LoginDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.IApUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.utils.common.AppJwtUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * APP用户信息表 服务实现类
 * </p>
 *
 * @author lcz
 * @since 2024-11-15
 */
@Service
public class ApUserServiceImpl extends ServiceImpl<ApUserMapper, ApUser> implements IApUserService {

    @Override
    public ResponseResult login(LoginDto dto) {
        ApUser apUser = lambdaQuery().eq(ApUser::getPhone, dto.getPhone()).one();
        if(apUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST);
        }
        String salt = apUser.getSalt();
        String password = apUser.getPassword();
        String md5DigestAsHex = DigestUtils.md5DigestAsHex((dto.getPassword()+salt).getBytes());
      if(!md5DigestAsHex.equals(password)){
          return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST);
      }
      apUser.setSalt(null);
      apUser.setPassword(null);
        Map<String,Object> map=new HashMap<>();
      map.put("user",apUser);
      map.put("token", AppJwtUtil.getToken(apUser.getId().longValue()));
        return ResponseResult.okResult(map);
    }
}
